import 'package:flutter/cupertino.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';

import '../../../data/db/models/bt_order.dart';
import '../../../data/db/providers/bt_order_provider.dart';

class BtMyTicketModel extends ChangeNotifier {
  final int _pageSize = 20;

  final PagingController<int, BtOrder> _pagingController = PagingController(firstPageKey: 0);

  PagingController<int, BtOrder> get pagingController => _pagingController;

  late PageRequestListener<int> _listener;

  final BtOrderProvider orderProvider = BtOrderProvider();

  BtMyTicketModel() {
    _listener = (pageKey) {
      pageKey++;
      fetchBtOrderList(pageKey);
    };
    _pagingController.addPageRequestListener(_listener);
  }

  void fetchBtOrderList(int pageKey) {
    orderProvider.queryAll().then((orders) {
      try {
        final newItems = orders;
        final isLastPage = newItems.length < _pageSize;
        if (isLastPage) {
          _pagingController.appendLastPage(newItems);
        } else {
          _pagingController.appendPage(newItems, pageKey);
        }
      } catch (error) {
        _pagingController.error = error;
      }
    });
  }

  void copy(Function(bool success, String msg) onResult) {
    //repository.copy( onResult);
  }
}
